<?php

class Webmix_Model_DbTable_Webmixes extends Engine_Db_Table {
	protected $_rowClass = 'Webmix_Model_Webmix';
    
    public function existUserDefault($user_id ){
        $select = $this->select()
                       ->where('owner_id = ?',$user_id)
                       ->where('type = ?','user')
                       ->where('`default` = ?',1);
                      
        $row = $this->fetchRow($select);
        if($row) return true;
        return false;
                        
    }
    public function getWebmixsByUser($user_id = 0){
        
        if(empty($user_id)){
            $user_id = Engine_Api::_()->user()->getViewer();
            $user_id = $user_id->user_id;
            if(empty($user_id)) return null;
        }
        $deleteAdminDefault = $this->isDeletedAdminDefault($user_id);
        
        $webmixDefaultContry = Engine_Api::_()->getApi('settings','core')->getSetting('webmix.default.contry',0);
        $user_country = 0;
        
        $sql = "(owner_id = {$user_id} and `type` = '".Webmix_Model_Webmix::WEBMIX_TYPE_USER."') ";
        if(!$deleteAdminDefault){
            $sql = '( '.$sql.' or '."(`type` ='".Webmix_Model_Webmix::WEBMIX_TYPE_SYSTEM."' and `default`=1 and public = 1)" .' )';
        }
        
         
        $select = $this->select()
                       ->where($sql)
                       ->order('order asc')
                       ->order('type asc')
                       ->order('default desc');
        $unpublic = $this->getWebmixNotPublic();
        if(count($unpublic)){
            $select->where('parent_id not in (?)',$unpublic);
        }           
        $rows = $this->fetchAll($select);
        
        return $rows; 
         
    }
    public function getWebmixNotPublic(){
        $select = $this->select()
                       ->from(array('webmix'=>$this->info('name')),array('webmix_id'))
                       ->where("type = 'system' ")
                       ->where('public = 0');
        $rows = $this->fetchAll($select);
        $result = array();
        if($rows) {
            foreach($rows as $row){
                $result[] = $row->webmix_id;
            }
        }
        return $result;
    }
    
    public function isDeletedAdminDefault($user_id){
        $deleteT = Engine_Api::_()->getDbtable('deletes','webmix');
        $select = $deleteT->select()
                          ->where('user_id = ?',$user_id);
        $row = $deleteT->fetchRow($select);
        if($row)  return true;
        return false;
    }
    
    public function getTotalWebmix($user_id = null){
        $select = $this->select()->from($this->info('name'),'count(webmix_id) as totalwebmix');
        if(!empty($user_id) && !is_null($user_id)) $select->where('owner_id = ?',$user_id);
        $select->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_USER);
        $rows = $this->fetchRow($select);
        if(!$rows) return 0;
        return $rows->totalwebmix;
    }
    
    public function userCountry($user_id){
        $select = $this->select()->where('owner_id = ?',$user_id)->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_USER);
        $rows = $this->fetchAll($select);
        $result = array();
        if($rows){
            foreach($rows as $item){
                $result[] = $item->country_id;
            }
        }
        return $result;
    }
    
    public function hadWebixId($user,$webmix_id){
        $select = $this->select()->where('owner_id = ?',$user)->where('type = ?',Webmix_Model_Webmix::WEBMIX_TYPE_USER)->where('parent_id = ?',$webmix_id);
        $row = $this->fetchRow($select);
        if($row) return $row->webmix_id;
        return false;
    }
    
    public function unsetDefault($user_id,$webmix_id){
        $this->update(
            array(
                'default' => 0
            ),
            array(
                'owner_id = ?' =>$user_id,
                'type = ?'=>'user'
            )
        );
        
    }
 }